//
//  JCMCLDataSource.m
//  PYQDemo
//
//  Created by Guo.JC on 2017/9/6.
//  Copyright © 2017年 coollang. All rights reserved.
//

#import "JCMCLDataSource.h"
#import "JCCommentCell.h"
#import "JCMomentsModel.h"
#import "JCMomentSetting.h"


@interface JCMCLDataSource ()

@end

@implementation JCMCLDataSource

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _sourceArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    JCCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"JCCommentCell" forIndexPath:indexPath];
    [cell setMomentResponseModel:_sourceArray[indexPath.row]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    JCMomentResponseModel *model = _sourceArray[indexPath.row];
    UserInforItem *userInfoItem = [User getLastUser].userInforItem;
    BOOL isMyComment = [model.rUserID isEqualToString:userInfoItem.userID];
    BOOL isMyTopic = [model.momentModel.userID isEqualToString:userInfoItem.userID];
    BOOL isResponseMyComment = [model.quote.userID isEqualToString:userInfoItem.userID];
    if (isMyTopic) {///是我发布的帖子
        if (isMyComment) {
            ///不能回复自己的评论
            [tableView deselectRowAtIndexPath:indexPath animated:NO];
        }else{
            ///只能回复自己帖子下，别人的评论
            [tableView deselectRowAtIndexPath:indexPath animated:YES];
            [[NSNotificationCenter defaultCenter] postNotificationName:kNoticePopInputView object:nil userInfo:@{@"model":_sourceArray[indexPath.row]}];
        }
    }else{///不是我发布的帖子
        if (isResponseMyComment) {
            ///可以回复主人回复自己的评论
            [tableView deselectRowAtIndexPath:indexPath animated:YES];
            [[NSNotificationCenter defaultCenter] postNotificationName:kNoticePopInputView object:nil userInfo:@{@"model":_sourceArray[indexPath.row]}];
        }else if (isMyComment){
            ///不能回复自己的评论
            [tableView deselectRowAtIndexPath:indexPath animated:NO];
        }
        else{
            ///可以回复别人的评论及回复
            [tableView deselectRowAtIndexPath:indexPath animated:YES];
            [[NSNotificationCenter defaultCenter] postNotificationName:kNoticePopInputView object:nil userInfo:@{@"model":_sourceArray[indexPath.row]}];
        }
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return ((JCMomentResponseModel *)_sourceArray[indexPath.row]).commentHeight;
}


@end
